package com.example.shushiyunsst.widget.verification.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SeekBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.example.library_common.glidmodule.GlideApp;
import com.example.shushiyunsst.R;

import java.util.List;

/**
 * Created by zhoulikai on 17/4/18.
 * 拖拽View
 */

public class DragVerificationView extends BaseVerificationView implements SeekBar.OnSeekBarChangeListener,
        SwipeCaptchaView.OnCaptchaMatchCallback, View.OnClickListener {
    private SwipeCaptchaView mSwipeCaptchaView;
    private SeekBar mSeekBar;
    private List<String> imageList;
    private int imageIndex;
    private VerificationListener listener;

    public DragVerificationView(Context context) {
        super(context);
    }

    @Override
    protected int setVerificationViewId() {
        return R.layout.drag_verification_layout;
    }

    @Override
    protected void initView(Context context, AttributeSet attrs) {
        mSwipeCaptchaView = (SwipeCaptchaView) findViewById(R.id.swipeCaptchaView);
        mSeekBar = (SeekBar) findViewById(R.id.dragBar);

        mSeekBar.setOnSeekBarChangeListener(this);
        mSwipeCaptchaView.setOnCaptchaMatchCallback(this);
        Button btnCancel = (Button) this.findViewById(R.id.btn_cancel);
        btnCancel.setOnClickListener(this);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        mSwipeCaptchaView.setCurrentSwipeValue(progress);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        mSeekBar.setMax(mSwipeCaptchaView.getMaxSwipeValue());
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        mSwipeCaptchaView.matchCaptcha();
    }

    @Override
    public void matchSuccess(SwipeCaptchaView swipeCaptchaView) {
        swipeCaptchaView.resetCaptcha();
        mSeekBar.setProgress(0);
        if (this.listener != null) {
            this.listener.notifyStatusResult(VerificationListener.SUCCESS_STATUS);
        }
    }

    @Override
    public void matchFailed(SwipeCaptchaView swipeCaptchaView) {
//        mSeekBar.setEnabled(false);
        swipeCaptchaView.resetCaptcha();
        mSeekBar.setProgress(0);
        if (this.listener != null) {
            this.listener.notifyStatusResult(VerificationListener.FAIL_STATUS);
        }
    }


    @Override
    public void setData(ViewGroup view, List<String> imageList, VerificationListener listener) {
        this.imageList = imageList;
        this.listener = listener;
        if (imageList != null && !imageList.isEmpty()) {
            imageIndex = 0;
            refreshImage();
        }
    }

    private void refreshImage() {
        String url = "";
        if (imageList != null && !imageList.isEmpty()) {
            if (imageIndex >= 0 && imageIndex < imageList.size()) {
                url = imageList.get(imageIndex);
            }
        }
        GlideApp.with(this.getContext())
                .asBitmap()
                .load(url)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        mSwipeCaptchaView.setImageBitmap(resource);
                        mSwipeCaptchaView.createCaptcha();
                    }

                });
    }


    @Override
    public void onClick(View v) {
        if (this.listener != null) {
            this.listener.notifyStatusResult(VerificationListener.CACEL_STATUS);
        }
    }
}